#include "LolTimer.h"

LolTimer::LolTimer(MainWindow * window, QObject *parent) :QObject(parent){
    this -> window = window;
    this -> settings = new Settings(QString(":/INI/Resources/INI/Settings.ini"));
    this -> checkProcess = new CheckProcess(settings->getValue("readTimeout").toInt());

    this -> window -> setWindowTitle(this -> settings -> getValue("applicationName").toString());
    this -> window -> setRevision(this -> settings -> getValue("revisionNumber").toInt());

    initOptions();

    this -> overlayWindow = new OverlayWindow(this->settings, this->window);


    connect(this -> checkProcess, SIGNAL(signalProcessStarted(Process * )), this, SLOT(slotProcessStarted(Process *)));
    connect(this -> checkProcess, SIGNAL(signalProcessStopped(Process * )), this, SLOT(slotProcessStopped(Process *)));
    connect(this -> window -> getUi() -> actionForceStart, SIGNAL(triggered()), this, SLOT(slotForceStartOverlay()));
    connect(this -> window -> getUi() -> actionForceStop, SIGNAL(triggered()), this, SLOT(slotForceStopOverlay()));
}


LolTimer::~LolTimer(){
    delete this->overlayWindow;
    delete this->settings;
    delete this->checkProcess;
}


void LolTimer::slotForceStartOverlay(){
    openOverlayWindow();
}


void LolTimer::slotForceStopOverlay(){

    closeOverlayWindow();
}


void LolTimer::slotStart(){
    start();
}

void LolTimer::start(){
    startProcessCheck();
}


void LolTimer::stopProcessCheck(){
    this -> checkProcess -> stop();
}

void LolTimer::startProcessCheck(){
    this -> checkProcess -> start();
}

void LolTimer::slotProcessStopped(Process * process){
    this -> window -> log(QString("Process"), process -> getExeName() + " stopped");
    if (process -> getExeName() == QString("League of Legends.exe")){
        closeOverlayWindow();
    }
}

void LolTimer::slotProcessStarted(Process * process){
    this -> window -> log(QString("Process"), "(ID : "+ QString::number(process->getId()) + ", Threads : " + QString::number(process-> getThreadsNumber()) + ", Priority : " + QString::number(process->getPriority()) + ") " + process -> getExeName() + " started");
    if (process -> getExeName() == QString("League of Legends.exe")){
        openOverlayWindow();
    }
}


void LolTimer::openOverlayWindow(){
    window->getUi()->actionForceStart->setEnabled(false);
    window->getUi()->actionForceStop->setEnabled(true);
    this -> overlayWindow -> openWindow();
}


void LolTimer::closeOverlayWindow(){
    window->getUi()->actionForceStart->setEnabled(true);
    window->getUi()->actionForceStop->setEnabled(false);
    this -> overlayWindow -> closeWindow();
}

void LolTimer::initOptions(){
    this->window->getUi()->readTimeout ->setValue(this->settings->getValue("readTimeout").toInt());
    this->window->getUi()->checkBoxBackground->setChecked(this->settings->getValue("transparentBackground").toBool());
    this->window->getUi()->checkBoxFading->setChecked(this->settings->getValue("fading").toBool());
    connect(this->window->getUi()->readTimeout, SIGNAL(valueChanged(int)), this, SLOT(slotChangeReadTimout(int)));
    connect(this->window->getUi()->checkBoxFading, SIGNAL(toggled(bool)), this, SLOT(slotChangeFading(bool)));
    connect(this->window->getUi()->checkBoxBackground, SIGNAL(toggled(bool)),this, SLOT(slotChangeBackground(bool)));
}



void LolTimer::slotChangeFading(bool value){
    this->settings->setValue("fading", value);
}

void LolTimer::slotChangeBackground(bool value){
    this->settings->setValue("transparentBackground", value);
    this->overlayWindow->setTransparentBackground(value);
}



void LolTimer::changeReadTimeout(int selectedTimeout){
    this -> settings -> setValue("readTimeout", selectedTimeout);
    this -> checkProcess -> setRefreshRate(selectedTimeout);
    stopProcessCheck();
    startProcessCheck();
    this -> window -> setStatusBar(QString("Timeout changed for ") + QString::number(selectedTimeout) + " ms");
}



void LolTimer::slotChangeReadTimout(int selectedTimeout){
    changeReadTimeout(selectedTimeout);

}



void LolTimer::msleep(unsigned long msecs){
    QTimer::singleShot(msecs, this, SIGNAL(signalSleepEnd()));
    QEventLoop loop;
    QObject::connect(this, SIGNAL(signalSleepEnd()), &loop, SLOT(quit()));
    loop.exec();
}
